package com.dormitory.javadormitory.controller;

import com.dormitory.javadormitory.common.Result;
import com.dormitory.javadormitory.entity.Student;
import com.dormitory.javadormitory.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/man")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    //查询全部男生
    @GetMapping("/arr")
    public Result arr() {
        ArrayList<Student> arr = userInfoService.arr();
        return Result.success(arr);
    }

    //添加
    @PostMapping("/insertVerify")
    public Result insertVerify(@RequestBody Student student) {
        //判断添加内容是否有空
        if ((student.getName().equals("")) || (student.getBunkhouseName().equals("")) || (student.getEnrollmentTime().equals(""))){
            return Result.success("内容不能有空");
        }
        userInfoService.insertVerify(student);
        return Result.success();
    }

    //id查询要修改的数据
    @GetMapping("/updateById")
    public Result updateById(int id) {
        Student student = userInfoService.updateById(id);
        return Result.success(student);
    }

    //修改id数据
    @PutMapping("/updateStudent")
    public Result updateStudent(@RequestBody Student student) {
        if ((student.getName().equals("")) || (student.getBunkhouseName().equals("")) || (student.getEnrollmentTime() == null)){
            return Result.success("内容不能有空");
        }
        userInfoService.updateStudent(student);
        return Result.success();
    }

    @GetMapping("/studentDeleteD")
    public Result studentDeleteD(int id) {
        userInfoService.studentDeleteD(id);
        return Result.success();
    }

    //根据名字查询
    @GetMapping("/onSubmitName")
    public Result onSubmitName(String name) {
        ArrayList<Student> arr = userInfoService.onSubmitName(name);
        return Result.success(arr);
    }

    //bunkhouseName
    @GetMapping("/onSubmitBunkhouseName")
    public Result onSubmitBunkhouseName(String bunkhouseName) {
        ArrayList<Student> arr = userInfoService.onSubmitBunkhouseName(bunkhouseName);
        return Result.success(arr);
    }

}
